/* Author: yangzeming
 * Date: 2013-12-10
 * Desc: 遮罩层
 */
(function($, undefined){

    /**
     * 弹出页面遮罩层
     * @class
     * @constructor
     * @param options 通过设定的options，使窗口响应鼠标的拖拽
     * @param {String} options.className 遮罩层类名, 默认为 "dialogJmodal"
     * @param {Boolean} options.resizeable 是否随窗口改变大小, 默认为true
     * @param {Boolean} options.show 初始化时是否显示, 默认为false
     * @alias $.modal
     */

	var modal = _.Class({
		initial : function(options){		
			/*
			 * options介绍
			 * className: 'dialogJmodal' //遮罩层类名
			 * safety: null, //安全容器，IE6-，当SELECT,OBJECT,EMBED标签在这个容器内，弹出遮罩时，容器内的标签不会被隐藏
			 * show: true, //初始化时自动显示
			*/	
			
			var self = this;
			this.cfg = $.extend({},{className: 'dialogJmodal', resizeable: true}, options);
			this.element = $('<div class="' + this.cfg.className + '"/>').appendTo(document.body).css({'display': 'none','zIndex' : $.getzIndex(), width: this.width(), "height" : this.height()});
			if(this.cfg.show)
				this.show();
			this.resizeFunc = function(){
				self.css("width", self.width());
				self.css("height", self.height());
				self.trigger('resize');
			};
			
			//绑定resize事件
			if(this.cfg.resizeable)
				$(window).bind('resize', this.resizeFunc);      
		
		},
			 /**
         * 显示遮罩层
         */
		show: function(){
			this.element.show.apply(this.element, arguments);
			this._processTages(1);
		},
        /**
         * 隐藏
         */
		hide: function(){
			this.element.hide.apply(this.element, arguments);
			this._processTages(0);
		},
        /**
         * 获得遮罩层的宽度
         * @return {Number}
         */
		width: function() {
			return $('body').width();
		},
        /**
         * 获得遮罩层的高度
         * @return {Number}
         */
		height: function() {
			return Math.max($('body').height(),$('html').height());
		},
        /**
         * 与jQuery#css功能相同
         * @param {String|Object} name 样式名称, 或是名称, 值对
         * @param {String} [value] 样式值
         */
		css: function(){
				this.element.css.apply(this.element, arguments);
		},
        
        /**
         * 析构
         */
		remove: function(){
			this._processTages(0);
			this.element && this.element.remove();
			$(window).unbind('resize', this.resizeFunc);
			for(var i in this)
				delete this[i];
		},
		_processTages: function(isHide){

			var self = this;

			if(!$.browser.msie) 
	    		return;
	    		
	       	
	       	self.special = self.special || [];	
	       	if(isHide){
	       		if(self.special.length > 0)
	       			return;
	       		//备份样式
				var doms = $("SELECT,OBJECT,EMBED");
	       			       				       	
				if(this.cfg.safety){
					doms = doms.filter(function(index){
						return self.cfg.safety.find(this).length == 0;
					});	  
					      	
				}
	       		doms.each(function(){
	       			var obj = $(this);

       				self.special.push({dom: this, css: obj.css('visibility')});
       				obj.css('visibility', 'hidden');
	       		})
	       	}
	       	else{
	       		for(var i = 0, len = self.special.length; i < len; i++){
	       			$(self.special[i].dom).css('visibility', self.special[i].css || ''); 
	       			self.special[i].dom = null;
	       		}
	       }			
		}
	
	});

	
	$.modal = modal;
    /**
     * 获取全局zIndex值,基数为50000
     * @return {Number}
     */
	$.getzIndex = function(){
		 $.zIndex = ($.zIndex || 50000);
		 return $.zIndex++;
	}

})(jQuery);